package com.railway.servlet;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.railway.service.AdminService;
import com.railway.service.impl.AdminServiceImpl;

/**
 * Servlet implementation class AdminLoginServlet
 */
@WebServlet("/AdminLoginServlet.do")
public class AdminLoginServlet extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String admin_name = "";
		String admin_password = "";
		if(request.getParameter("admin_name") != null && !"".equals(request.getParameter("admin_name"))) {
			admin_name = request.getParameter("admin_name");
		}
		if(request.getParameter("admin_password") != null && !"".equals(request.getParameter("admin_password"))) {
			admin_password = request.getParameter("admin_password");
		}
		
		
		AdminService as = new AdminServiceImpl();
		Map map = as.login(admin_name,admin_password);
		Set<String> map2 = map.keySet();
		String temp = "";
		for (String m : map2) {
			temp = m;
		}
		if("longinAdmin".equals(temp)) {
			request.getSession().setAttribute("loginAdmin",admin_name);
			request.getSession().setMaxInactiveInterval(3000);
			/* response.sendRedirect("menu.jsp"); */
			request.getRequestDispatcher("menu.jsp").forward(request, response);
			return ;
		}else {
			response.sendRedirect("loginAdmin.html");
			return ;
		}
	}

}
